home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mntlb20 / lib / getlogin.c < prev    next >
C/C++ Source or Header  |  1991-10-19  |  1KB  |  48 lines

  1. /* getlogin: return the user's login name.
  2.  * Written by Eric R. Smith and placed in the public domain.
  3.  */
  4.  
  5. #include <pwd.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <memory.h>
  9. #include <unistd.h>
  10.  
  11. #ifdef __GNUC__
  12. #define alloca __builtin_alloca
  13. #endif
  14.  
  15. static char *logname = NULL;
  16.  
  17. char *getlogin()
  18. {
  19.         struct passwd *temp;
  20.     char *tmplogname;
  21.  
  22.     if(logname != NULL)
  23.         return logname;
  24.  
  25.     tmplogname = (char *)alloca((size_t)80);
  26.     *tmplogname = '\0';
  27.  
  28. /* first try the /etc/passwd file */
  29.         temp = getpwuid(getuid());
  30.         if (temp) {
  31.           strncpy(tmplogname, temp->pw_name, (size_t)79);
  32.       tmplogname[79] = '\0';
  33.     }
  34.  
  35. /* if that didn't work, try the environment */
  36.         if (!*tmplogname && getenv("USER")) {
  37.                 strncpy(tmplogname, getenv("USER"), (size_t)79);
  38.         tmplogname[79] = '\0';
  39.         }
  40.  
  41. /* finally, give up */
  42.         if (!*tmplogname)
  43.                 strcpy(tmplogname, "user");
  44.     if((logname = (char *)malloc((size_t)(strlen(tmplogname)+1))) == NULL)
  45.         return (char *)NULL;
  46.         return strcpy(logname, tmplogname);
  47. }
  48.